package com.baijiayun.liveuibase.widgets.common;

import android.content.Context;
import android.graphics.drawable.StateListDrawable;
import androidx.core.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.Switch;

import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.DisplayUtils;
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder;

import static com.baijiayun.liveuibase.utils.ThemeDataUtil.getColorFromThemeConfigByAttrId;

public class CommonThemeSwitch extends Switch {

    private StateListDrawable trackDrawable;

    public CommonThemeSwitch(Context context) {
        super(context);
        initDrawable();
    }

    public CommonThemeSwitch(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDrawable();
    }

    public CommonThemeSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDrawable();
    }

    private void initDrawable() {
        trackDrawable = new StateListDrawable();
        trackDrawable.addState(
                new int[]{android.R.attr.state_checked}
                , new DrawableBuilder()
                        .solidColor(getColorFromThemeConfigByAttrId(getContext(), R.attr.base_theme_live_product_color))
                        .cornerRadius(DisplayUtils.dip2px(getContext(), 50))
                        .build()
        );
        trackDrawable.addState(
                new int[]{-android.R.attr.state_checked}
                , new DrawableBuilder()
                        .solidColor(getColorFromThemeConfigByAttrId(getContext(), R.attr.base_theme_bottom_menu_text_color))
                        .cornerRadius(DisplayUtils.dip2px(getContext(), 50))
                        .build()
        );
        setThumbDrawable(ContextCompat.getDrawable(getContext(), R.drawable.bjy_base_shape_common_switch_thumb));
        setTrackDrawable(trackDrawable);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        trackDrawable.setAlpha(enabled ? 0xff : 0x80);
    }
}
